import { SEVERE_RES, SEVERE_INFO, CHANGE_PRODUCT_COUNT, CHANGE_PRODUCT_HASSELETED, CHECK_ALL_SELECTED_PRODUCT } from './mutations-type'

export default {
  [SEVERE_RES](state, { res }) {
    state.res = res
  },
  [SEVERE_INFO](state, { products }) {
    products.forEach(product => {
      product.hasSelected = false
    })
    state.products = products
  },
  [CHANGE_PRODUCT_COUNT](state, { productId, type }) {
    console.log(productId, type)
    let index = state.products.findIndex(e => e.productId === productId)
    console.log('11', index)
    if (type === 'add') {
      state.products[index].count += 1
    } else {
      if (state.products[index].count > 0) {
        state.products[index].count -= 1
      }
    }
  },
  [CHANGE_PRODUCT_HASSELETED](state, { hasSelected, index }) {
    state.products[index].hasSelected = hasSelected
  },
  [CHECK_ALL_SELECTED_PRODUCT](state, allTrue) {
    for (let i = 0; i < state.products.length; i++) {
      if (allTrue) {
        state.products[i].hasSelected = true
      } else {
        state.products[i].hasSelected = false
      }
    }
  },
}
